package com.atguigu.netty.protocoltcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {
    private int count;
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol message) throws Exception {
        //接收到数据并处理
        int length = message.getLen();
        byte[] content = message.getContent();

        System.out.println("服务端接收到信息如下");
        System.out.println("长度="+length);
        System.out.println("内容="+new String(content, StandardCharsets.UTF_8));
        System.out.println("服务器接收到消息包数量="+(++this.count));

        //回复消息
        String responseContent= UUID.randomUUID().toString();
        int responseLength = responseContent.getBytes().length;
        byte[] bytes = responseContent.getBytes(StandardCharsets.UTF_8);
        MessageProtocol messageProtocol = new MessageProtocol();    
        messageProtocol.setLen(responseLength);
        messageProtocol.setContent(bytes);
        channelHandlerContext.writeAndFlush(messageProtocol);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
